package it.docos;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import javax.ws.rs.core.Response;

import org.alfresco.repo.processor.BaseProcessorExtension;

import sun.misc.BASE64Encoder;

/**
 * Permette alle classi di Alfresco di effettuare chiamate remote.
 * 
 * @author Domenico Costa
 * 
 */
public class RemoteCall extends BaseProcessorExtension {

	public RemoteCall() {

	}

	/**
	 * Effettua una connessioen di tipo Get
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public String getHttpGet(String url) throws IOException {

		String resultCode = "";
		String urlEconde = URLEncoder.encode(url , "UTF8");
		
		
		URL urlCall;
		HttpURLConnection connection = null;

		urlCall = new URL(url);
		connection = (HttpURLConnection) urlCall.openConnection();
		connection.setRequestMethod("GET");

		// Legge la rispsota
		InputStream is;
		if (connection.getResponseCode() == 200) {
			is = connection.getInputStream();
		} else {
			is = connection.getErrorStream();
		}

		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		String line;
		StringBuffer response = new StringBuffer();
		while ((line = rd.readLine()) != null) {
			response.append(line);
		}
		rd.close();
		is.close();

		resultCode = String.valueOf(connection.getResponseCode());

		if (connection != null) {
			connection.disconnect();
		}

		return resultCode;
	}

	/**
	 * Effettua una connessione con autenticazione
	 * 
	 * @param url
	 * @param username
	 * @param password
	 * @return
	 * @throws IOException
	 */
	public String getHttpGet(String url, String username, String password)
			throws IOException {

		String resultCode = "";
		String urlEconde = URLEncoder.encode(url , "UTF8");
		

		URL urlCall;
		HttpURLConnection connection = null;

		urlCall = new URL(url);
		connection = (HttpURLConnection) urlCall.openConnection();
		connection.setRequestMethod("GET");

		// Imposto autenticazione
		BASE64Encoder enc = new sun.misc.BASE64Encoder();
		String userpassword = username + ":" + password;
		String encodedAuthorization = enc.encode(userpassword.getBytes());
		connection.setRequestProperty("Authorization", "Basic "
				+ encodedAuthorization);

		// Legge la rispsota
		InputStream is;
		if (connection.getResponseCode() == Response.Status.ACCEPTED.getStatusCode()) {
			is = connection.getInputStream();
		} else {
			is = connection.getErrorStream();
		}

		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		String line;
		StringBuffer response = new StringBuffer();
		while ((line = rd.readLine()) != null) {
			response.append(line);
		}
		rd.close();
		is.close();

		resultCode = String.valueOf(connection.getResponseCode());

		if (connection != null) {
			connection.disconnect();
		}

		return resultCode;
	}
	
	
	/**
	 * Effettua una connessioen di tipo Get
	 * 
	 * @param url
	 * @return
	 * @throws IOException
	 */
	public String getHttpGet(String url, String username, String message, String type, String tokenAuth) throws IOException {

		String resultCode = "";
		String messageEncode = URLEncoder.encode(message , "UTF8");
		
		String urlConnection = url +  "message=" + messageEncode +"&type=" + type + "&username=" + username + "&p_p_auth=" + tokenAuth;
		
		URL urlCall;
		HttpURLConnection connection = null;

		urlCall = new URL(urlConnection);
		connection = (HttpURLConnection) urlCall.openConnection();
		connection.setRequestMethod("GET");

		// Legge la rispsota
		InputStream is;
		if (connection.getResponseCode() == 200) {
			is = connection.getInputStream();
		} else {
			is = connection.getErrorStream();
		}

		BufferedReader rd = new BufferedReader(new InputStreamReader(is));
		String line;
		StringBuffer response = new StringBuffer();
		while ((line = rd.readLine()) != null) {
			response.append(line);
		}
		rd.close();
		is.close();

		resultCode = String.valueOf(connection.getResponseCode());

		if (connection != null) {
			connection.disconnect();
		}

		return resultCode;
	}


}
